import { Blog } from "../model/Blog.js"

 

const fn_list=async (ctx, next) => {
    let blogs=await Blog.findAll();

    ctx.body = {
        code:1000,
        msg:'列表成功',
        data:blogs
    }
}

const fn_getById=async (ctx, next) => {
    ctx.body = '指定博客'
}

const fn_add=async (ctx, next) => {
    console.log(ctx.request.body);
    
    ctx.body = '新增'
}

const fn_edit=async (ctx, next) => {
    ctx.body = '修改指定博客';
}

const fn_del=async (ctx, next) => {
    ctx.body = '删除指定博客';

     
    // 获取前端传过来的数据
    // 处理相关业务逻辑
    // 返回处理过后的响应结果
    //获取id
    const id=ctx.params.id || 0

    //处理业务逻辑：删除数据库中对应id的记录
   let count=await Blog.destroy({
        where:{
            id:id
        }
    })
    //处理响应的数据
    let res
    if(count===1){
        res={
            code:1000,
            msg:'删除成功',
            data:null
        }
    }else{
        res={
            code:4000,
            msg:'删除失败',
            data:null
        }
    }
    
    ctx.body=res
    
    

}

// 导出路由结构
// 导出路由结构
export default [
    {
       method: 'get',
       url: '/blogs',
       fn: fn_list
    },
    {
        method: 'get',
        url: '/blogs/:id', 
        fn: fn_getById 
    },
    {
        method: 'post',
        url: '/blogs',
        fn: fn_add
    }, 
    {
        method: 'put',
        url: '/blogs/:id', 
        fn: fn_edit
    }, 
    {
        method: 'del', 
        url: '/blogs/:id', 
        fn: fn_del
    }
];


